<?php
//+--------------------------------------------
//|描述  异步Mysql
//|--------------------------------------------
//|Created by PhpStorm.
//| Author: DRG
//+-------------------------------------------
//|Date: 2018/5/29
//| Time: 14:33
//+--------------------------------------------

class AysMysql {
    
    public $dbSource = "";
    public $dbConfig = [];
    public function __construct($host, $port, $user_name, $pwd, $dbname)
    {
        $this->connection = new Swoole\Mysql;
        $this->config = [
            'host'          => $host,
            'port'          => $port,
            'user'          => $user_name,
            'password'      => $pwd,
            'database'      => $dbname,
            'charset'       => 'utf8'
        ];
        
    }
    
    
    public function update(){
    
    }
    
    public function add()
    {
    
    }
    
    public function exceute($id, $username)
    {
        $this->connection->connect($this->config, function ($connect, $result) use ($id,$username) {
            //连接成功与否
            if($result === false) {
                var_dump($db->connect_error);
                //todo
            }
            // 连接成功
            $sql = 'select * from test where id =1 ';
    
            // $db 指的是 $this->>connect
            // query 方法能执行  select insert update delete
            $db->query($sql, function ($db, $result){// db   返回值
                //select   =>result  返回的是查询结果
                // add update delete 返回的是布尔值
                if($result === false) {
                    //todo
                    echo $db->error;// SQL语句错误
                }elseif ($result === true){//add update delete
                    //todo
                    // 受影响函数 $db -> affected_rows
                    // 获取自增id  $db -> insert_id
                }else{
                    //查询
                    var_dump($result);
                }
                // 执行完后，关闭连接
                $this->connection->close();
    
            });
        });
        
        return true;
    }
}

$host = '127.0.0.1';
$user_name = 'root';
$port = 3306;
$pwd = 123456;
$dbname = 'swoole';
$mysql = new AsyncMysql($host, $port, $user_name, $pwd, $dbname);
$flag = $mysql->execute(1,'ddd');

var_dump($flag).PHP_EOL;

/**
 * 场景  比如增加阅读说    musql 异步执行   不影响 代码业务逻辑继续往下执行
 */
